﻿#region Copyright
//————————————————————————————————————————//
//	创建人员(Creator)    ：Huangyd
//	创建日期(Date)       ：2012-7-2 14:31:05
//  联系方式(Email)      ：aj-ayumi@163.com; gajayumi@gmail.com; ajayumi@me.com
//  描    述(Description)：通用防SQL注入漏洞程序
//
//	Copyright (C) 2012 ajayumi 版权所有
//————————————————————————————————————————//
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace ajayumi.Platform.Web
{
    /// <summary>
    /// 通用防SQL注入漏洞赋值类
    /// </summary>
    public class SQLInjectionHelper
    {
        /// <summary>
        /// 获取Post的数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static bool ValidUrlData(string request)
        {
            bool result = false;

            if (request == "POST")
            {
                //获取Post的数据
                for (int i = 0; i < HttpContext.Current.Request.Form.Count; i++)
                {
                    result = ValidData(HttpContext.Current.Request.Form[i].ToString());
                    if (result)
                    {
                        break;
                    }
                }
            }
            else
            {
                //获取QueryString中的数据
                for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++)
                {
                    result = ValidData(HttpContext.Current.Request.QueryString[i].ToString());
                    if (result)
                    {
                        break;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 验证是否存在注入代码
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        private static bool ValidData(string inputData)
        {
            //验证inputData是否包含恶意集合
            return Regex.IsMatch(inputData, GetRegexString(), RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 获取正则表达式
        /// </summary>
        /// <returns></returns>
        private static string GetRegexString()
        {
            //构造SQL的注入关键字
            string[] strBadChar = { "and","exec","insert","select","delete","update","count","from","drop","asc",
                                    "char","or","%",";",":","\'","\"","-","chr","mid","master","truncate","char",
                                    "declare","SiteName","net user","xp_cmdshell","/add","exec master.dbo.xp_cmdshell",
                                    "net localgroup administrators"};
            //构建正则表达式
            StringBuilder str_Regex = new StringBuilder();
            str_Regex.Append(".*(");
            for (int i = 0; i < strBadChar.Length; i++)
            {
                str_Regex.AppendFormat("{0}|",strBadChar[i]);
            }
            str_Regex.Remove(str_Regex.Length - 1, 1);
            str_Regex.Append(").*");
            return str_Regex.ToString();
        }

    }
}
